import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class GsonTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// String json = "{\"date\":\"2014-05-12 21:23:43\"}";
		// System.out.println(json);
		//
		// Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		// .create();
		// Date d = gson.fromJson(json, Date.class);
		// System.out.println(d);

		// //////////////////////////////////////
		// List<User> list = new ArrayList<User>();
		// User u = new User();
		// u.name = "12321";
		// u.date = new Date();
		// User u1 = new User();
		// u1.name = "12322";
		// u1.date = new Date();
		// User u2 = new User();
		// u2.name = "12323";
		// u2.date = new Date();
		// list.add(u);
		// list.add(u1);
		// list.add(u2);
		// Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		// .create();
		// System.out.println(gson.toJson(list));
		// Type listType = new TypeToken<List<User>>() {
		// }.getType();
		//
		// List<User> users = gson.fromJson(gson.toJson(list), listType);
		// for (User m : users) {
		// System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
		// .format(m.date));
		// }
		// String json =
		// "{\"name\":\"12345\",\"date\":\"2013-01-04 10:30:30\"}";
		// // Gson gson = new Gson();
		//
		// System.out.println(gson.toJson(u));
		// User uu = gson.fromJson(json, User.class);
		// System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
		// .format(uu.date));
		// //////////////////////////////////////
		// String json =
		// "[{\"name\":\"12345\",\"date\":\"2013-01-04\"},{\"name\":\"12345\",\"date\":\"2013-01-04\"}]";
		// String s =
		// "[{\"name\":\"12345\",\"date\":{\"date\":28,\"day\":4,\"hours\":14,\"minutes\":46,\"month\":7,\"seconds\":34,\"time\":1309208394000,\"timezoneOffset\":-480,\"year\":114}}"
		// +
		// ",{\"name\":\"12345\",\"date\":{\"date\":28,\"day\":4,\"hours\":14,\"minutes\":46,\"month\":7,\"seconds\":34,\"time\":1409208394000,\"timezoneOffset\":-480,\"year\":114}}]";
		// // setJson(json);
		// setJson(s);
		// ////////////////////////////////////////
		// Gson g = new Gson();
		// Integer a = g.fromJson("1", Integer.class);
		// System.out.println(a);
		// ////////////////////////////////

		String s = "{\"value\":[{\"id\":\"2812cf58-de76-41f7-ab39-670613c8e568\" ,\"diaryIDs\":\"\",\"delAllow\":2,\"displaySeq\":null,\"name\":\"宝宝 日记\",\"creator\":null}],\"result\":null,\"auth\":null}";
		String s1 = s ;
//		String s1 = s.replace("\"\"", "null");
		String ss = "[{\"id\":\"2812cf58-de76-41f7-ab39-670613c8e568\" ,\"diaryIDs\":null,\"delAllow\":2,\"displaySeq\":null,\"name\":\"宝宝 日记\",\"creator\":null}]";
		System.out.println(s1);
		Gson gson = new GsonBuilder()
				.create();
		MobileDTO data = gson.fromJson(s1.toString(), MobileDTO.class);
		System.out.println(data.getValue().toString());
		List<Folder> d = gson.fromJson(gson.toJson(data.getValue()),
				new TypeToken<List<Folder>>() {
		}.getType());
		System.out.println(d.get(0).getName());
		System.out.println(d.toString());
	}

	public static List<User> setJson(String json) {
		Gson gson = null;
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new DateAdapter());
		// builder.setDateFormat("yyyy-MM-dd");
		gson = builder.create();
		List<User> list = null;
		Type type = new TypeToken<List<User>>() {
		}.getType();
		list = gson.fromJson(json, type);
		System.out.println(list);
		return list;
	}

}
